package com.study.jdbc.junit;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.junit.Test;

import com.study.jdbc.bean.Customer;
import com.study.jdbc.mydbutils.BeanHandle;
import com.study.jdbc.mydbutils.BeanListHandle;
import com.study.jdbc.mydbutils.JDBCAssist;
import com.study.jdbc.utils.DBCPUtil;

/**
* @Name: MyDBUtilsTest
* @Description: 自定义MyDBUtils工具测试类
* @Author: 李超	lc_jack@foxmail.com
* @CreateDate: 2017-2-19 下午10:52:58 
* @Version: V1.0
 */
public class MyDBUtilsTest {
	
	private JDBCAssist jdbcAssist = new JDBCAssist(DBCPUtil.getDataSource()) ;
	
	/**
	* @Name: queryOneRecord
	* @Description: 查询一条记录
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-2-19 下午10:54:08
	* @Parameters: 
	* @Return: void
	 */
	@Test
	public void queryOneRecord() {
		String sql = "select id, name, gender, birthday, cellphone, email, preference, type, description " +
				"from t_customer " +
				"where id = ? ; " ;
		String id = "146dedb9-ca8f-4ad4-991e-706380ff59c4" ;
		Customer customer = (Customer) jdbcAssist.query(sql, new BeanHandle(Customer.class), id) ;
		System.out.println(customer);
	}
	
	/**
	* @Name: queryListRecord
	* @Description: 查询多条记录
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-2-19 下午10:58:23
	* @Parameters: 
	* @Return: void
	 */
	@Test
	public void queryListRecord() {
		String sql = "select id, name, gender, birthday, cellphone, email, preference, type, description " +
				"from t_customer  ; " ;
		List<Customer> list = (List<Customer>) jdbcAssist.query(sql, new BeanListHandle(Customer.class)) ;
		for (Customer customer : list) {
			System.out.println(customer);
		}
	}
	
	/**
	* @Name: updateRecord
	* @Description: 更新记录操作
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-2-19 下午10:59:55
	* @Parameters: 
	* @Return: void
	 */
	@Test
	public void updateRecord() {
		String sql = "update t_customer set name = ? where id = ? ; " ;
		Customer customer = new Customer() ;
		customer.setId("2afb76ca-f2c1-4ef6-91e9-9c37f14fefe0") ;
		customer.setName("王五02") ;
		jdbcAssist.update(sql, 
				customer.getName(),
				customer.getId()) ;
	}
	
	/**
	* @Name: insertRecord
	* @Description: 新增记录操作
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-2-19 下午11:00:41
	* @Parameters: 
	* @Return: void
	 */
	@Test
	public void insertRecord() {
		String sql = "insert into t_customer(id, name, gender, birthday, cellphone, email, preference, type, description)" +
				"values(?, ?, ?, ?, ?, ?, ?, ?, ?) ;" ;
		Customer customer = new Customer() ;
		customer.setId(UUID.randomUUID().toString()) ;
		customer.setName("测试") ;
		customer.setGender("男") ;
		customer.setBirthday(new Date()) ;
		customer.setCellphone("1232131231") ;
		customer.setEmail("123@123.com") ;
		customer.setPreference("测试") ;
		customer.setType("普通客户") ;
		customer.setDescription("测试") ;
		jdbcAssist.update(sql, 
				customer.getId(),
				customer.getName(),
				customer.getGender(),
				customer.getBirthday(),
				customer.getCellphone(),
				customer.getEmail(),
				customer.getPreference(),
				customer.getType(),
				customer.getDescription()) ;
		System.out.println("插入成功！");
	}
	
	/**
	* @Name: deleteRecord
	* @Description: 删除记录操作
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-2-19 下午11:00:49
	* @Parameters: 
	* @Return: void
	 */
	@Test
	public void deleteRecord() {
		String sql = "delete from t_customer where id = ? ; " ;
		String id = "fafc3ff9-e030-4e06-9753-0d0bed2b56e5" ;
		jdbcAssist.update(sql, id) ;
		System.out.println("删除成功！");
	}

}

